package com.lanou.myapp.news.gamanews;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.lanou.myapp.R;
import com.lanou.myapp.imageloader.VolleySingleton;

import java.util.List;


/**
 * Created by dllo on 16/1/13.
 */
public class RecyclerGameAdapter extends RecyclerView.Adapter<RecyclerGameAdapter.MyViewHolder> {
    private Context context;
    private List<GameNews> data;
    private ImageLoader imageLoader;
    private RequestQueue queue;
    private LruCache<String, Bitmap> lruCache;
    private ImageLoader.ImageCache imageCache;

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_game_news, null);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }


    public RecyclerGameAdapter(Context context, List<GameNews> data) {
        this.context = context;
        this.data = data;
//        init();
//        imageLoader = VolleySingleton.getInstance()._getImageLoader();
    }

//
//    private void init() {
//        queue = Volley.newRequestQueue(context);
//        lruCache  = new LruCache<String, Bitmap>(20);
//        imageCache = new ImageLoader.ImageCache() {
//            @Override
//            public Bitmap getBitmap(String url) {
//                return lruCache.get(url);
//            }
//
//            @Override
//            public void putBitmap(String url, Bitmap bitmap) {
//                lruCache.put(url, bitmap);
//            }
//        };
//
//        imageLoader = new ImageLoader(queue, imageCache);
//    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
//        String imgUrl = data.get(position).getThumbnail();
//        getBitmapCache(imgUrl, holder.imageView);
//        holder.networkImageView.setDefaultImageResId(R.mipmap.v6_boy_unseletor_icon);
//        holder.networkImageView.setErrorImageResId(R.mipmap.v5_icon_usercenter);
//        holder.networkImageView.setImageUrl(data.get(position).getThumbnail(), imageLoader);


        Glide.with(context).load(data.get(position).getThumbnail()).into(holder.imageView);


        holder.tv_head.setText(data.get(position).getSection_name());
        holder.tv_title.setText(data.get(position).getTitle());
        holder.tv_author.setText(data.get(position).getAuthor_name());
        holder.tv_num.setText(data.get(position).getHit_count_string());

    }

    @Override
    public int getItemCount() {
        return data.size();
    }


//    public void getBitmapCache(String imgUrl, ImageView imageView) {
//        ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView, R.mipmap.v6_boy_unseletor_icon,
//                R.mipmap.v6_boy_unseletor_icon);
//        imageLoader.get(imgUrl, listener, 200, 200);
//    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;
        private TextView tv_head, tv_title, tv_author, tv_num;
        private NetworkImageView networkImageView;

        public MyViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.iv_game_news);
            tv_head = (TextView) itemView.findViewById(R.id.tv_head_game_news);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title_game_news);
            tv_author = (TextView) itemView.findViewById(R.id.tv_author_game_news);
            tv_num = (TextView) itemView.findViewById(R.id.tv_reading_game_news);
        }
    }

}
